package com.fh.alipay.controller;

import com.fh.alipay.vo.ApiResult;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import com.fh.alipay.po.Order;
import com.fh.alipay.service.IOrderService;
import com.fh.alipay.model.dto.OrderDTO;
import com.fh.alipay.model.vo.OrderVO;
import com.fh.alipay.model.query.OrderQuery;
import com.fh.alipay.model.convert.OrderConvert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.Arrays;

import org.springframework.web.bind.annotation.RestController;

/**
 * @author fh
 * @date 2022-05-04
 * @description 订单表控制器
 */
@RestController
@RequestMapping("/aliPay/order")
public class OrderController extends BaseController {

    @Resource
    private IOrderService orderService;


    /**
     * 获取订单表分页列表
     */
    @PostMapping(value = "/list")
    public ApiResult<IPage<Order>> list(OrderQuery query, Page page) {
        IPage<Order> resultPage = orderService.getWithPage(page, query);
        return ApiResult.ok(resultPage);
    }

    /**
     * 根据ID获取订单表详情
     */
    @GetMapping("/get")
    public ApiResult<OrderVO> get(@RequestParam("id") Long id) {
        Order entity = orderService.getById(id);
        OrderVO vo = OrderConvert.toVO(entity);
        return ApiResult.ok(vo);
    }

    /**
     * 新增订单表
     */
    @PostMapping("/add")
    public ApiResult<String> add(OrderDTO dto) {
        Order po = OrderConvert.toPO(dto);
        po.setCreateTime(LocalDateTime.now());
        po.setUpdateTime(LocalDateTime.now());
        boolean save = orderService.save(po);
        return save ? ApiResult.ok() : ApiResult.fail();
    }

    /**
     * 编辑订单表
     */
    @PostMapping("/update")
    public ApiResult update(OrderDTO dto) {
        Order po = OrderConvert.toPO(dto);
        po.setUpdateTime(LocalDateTime.now());
        boolean update = orderService.updateById(po);
        return update ? ApiResult.ok() : ApiResult.fail();
    }

    /**
     * 移除订单表
     */
    @PostMapping("/remove")
    public ApiResult<String> remove(@RequestParam(value = "id") Long id) {
        boolean remove = orderService.removeById(id);
        return remove ? ApiResult.ok() : ApiResult.fail();
    }

    /**
     * 批量移除订单表
     */
    @PostMapping("/batch/remove")
    public ApiResult batchRemove(@RequestParam(value = "ids") String ids) {
        boolean remove = orderService.removeByIds(Arrays.asList(ids.split(",")));
        return remove ? ApiResult.ok() : ApiResult.fail();
    }
}
